//
//  PhoneYZMTableViewCell.m
//  Inforcollection
//
//  Created by macbook on 15/12/10.
//  Copyright © 2015年 oceansoft.liw. All rights reserved.
//

#import "PhoneYZMTableViewCell.h"

@implementation PhoneYZMTableViewCell

- (void)awakeFromNib {
    // Initialization code
    
    [self initCellAttribute];
    m = 60;
    
}

+ (instancetype)phoneYZMGetWithTableView:(UITableView *)tableView
{
    static NSString *cellID = @"CellID";
    PhoneYZMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"PhoneYZMTableViewCell" owner:nil options:nil] firstObject];
    }
    return cell;
}

//设置cell的基本属性
- (void)initCellAttribute
{

    self.YZMLabel.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:232.0/255.0 blue:232.0/255.0 alpha:1.0];
    
    //设置label为圆角
    self.YZMLabel.layer.cornerRadius = 5.0f;
    
    [[self.YZMLabel layer]setMasksToBounds:YES];
    
    self.YZMLabel.userInteractionEnabled = YES;
    
    self.YZMLabel.backgroundColor = [UIColor colorWithRed:93/255.0f green:129/255.0f blue:221/255.0f alpha:1];
    
    self.YZMLabel.textColor = [UIColor whiteColor];
    
    //右边的小 X
    self.YZMTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    self.YZMTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    
    self.YZMTextField.returnKeyType = UIReturnKeyDone;
    //text的边框设置为没有
    self.YZMTextField.borderStyle = UITextBorderStyleNone;
    
    self.YZMTextField.delegate = self;
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:232.0/255.0 blue:232.0/255.0 alpha:1.0];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.YZMTextField resignFirstResponder];
    
    return YES;
}
#pragma mark - 获得第二个界面的验证码
- (void)touchLabelGetYZM1
{
    UITapGestureRecognizer  *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTouchUpInside:)];
    tapGesture.numberOfTapsRequired = 1;
    tapGesture.numberOfTouchesRequired = 1;
    [self.YZMLabel addGestureRecognizer:tapGesture];
}

- (void)labelTouchUpInside:(UITapGestureRecognizer *)sender
{
    UILabel *label = (UILabel *)sender.view;
    
    NSLog(@"%ld点击了我",label.tag);
    
    NSString *telString = [self.YZMTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    if (telString.length == 0)
    {
        [MBProgressHUD showHUDAddedTo:SharedApplication.keyWindow text:@"请输入手机号码" duration:1.5 whileExecutingBlock:nil];
        return;
    }
    else if (telString.length!=11)
    {
        [MBProgressHUD showHUDAddedTo:SharedApplication.keyWindow text:@"请输入正确的手机号码" duration:1.5 whileExecutingBlock:nil];
        
        return;
    }
    else if (![telString isEqualToString:[UserData shareUserData].userModel.phone])
    {
        [MBProgressHUD showHUDAddedTo:SharedApplication.keyWindow text:@"请输入当前账号绑定的手机号码" duration:1.5 whileExecutingBlock:nil];
        
        return;
    }
    
    NSDictionary *postDic = @{@"tel":self.YZMTextField.text,@"type":[NSString stringWithFormat:@"%ld",label.tag-UserTag],@"content":@"治安寄递采集",@"expire":@"60"};
    
    [[HttpAPI shareHttpAPI] getYZMWithDic:postDic CallBack:^(NSDictionary *receivedData)
    {
        if (receivedData != nil)
        {
            [MBProgressHUD showHUDAddedTo:SharedApplication.keyWindow text:receivedData[@"msg"] duration:1.5 whileExecutingBlock:nil];
            if (m > 0)
            {
                self.YZMLabel.userInteractionEnabled = NO;
                
                [self getCode];
            }else
            {
                
            }
        }
    }];
}
- (void)getCode
{
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
- (void)timerRun
{
    if (m == 0) {
        self.YZMLabel.userInteractionEnabled = YES;
        self.YZMLabel.text = @"获取验证码";
        [timer invalidate];
        m = 60;
        return;
    }else
    {
        NSString *str = [NSString stringWithFormat:@"%d秒",m];
        self.YZMLabel.text = str;
        m--;
    }
}

#pragma  mark ----获得第三个界面的验证码
- (void)touchLabelGetYZM2
{
    UITapGestureRecognizer *tapLabel = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTouchGetYZM:)];
    tapLabel.numberOfTapsRequired = 1;
    tapLabel.numberOfTouchesRequired = 1;
    [self.YZMLabel addGestureRecognizer:tapLabel];
}

- (void)labelTouchGetYZM:(UITapGestureRecognizer *)sender
{
    UILabel *label = (UILabel *)sender.view;
    NSLog(@"danlewoa%@",label.text);
    if (m > 0)
    {
        self.YZMLabel.userInteractionEnabled = NO;
        
        [self getCode];
    }else
    {
        
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.YZMTextField resignFirstResponder];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
